<?php


class GroupController extends \Phalcon\Mvc\Controller
{
	
	public function acceptAction()
	{
		$request_id = $this->request->getPost('request_id');	

		$group_requests = GroupRequests::findFirstById($request_id);
		$group_id = $group_requests->group_id;
		$user_id = $group_requests->user_id;
		$result  = array();

		$group_info = Groups::findFirstById($group_id);

		if(!$group_requests->delete())
		{
			$result['status'] = false;
			echo json_encode($result);
			$this->view->disable();
			exit();
		}

		if(!GroupMembers::addMember($group_id,$user_id))
		{
			$result['status'] = false;
			echo json_encode($result);
			$this->view->disable();
			exit();
		}
		$data = array(
				"group_id"=>$group_id,
				"receiver_id"=>$user_id,
				"group_title"=>$group_info->title,
				"group_icon"=> (!$group_info->icon) ? IMAGE_URL_SMALL.DEFAULT_PIC : IMAGE_URL_SMALL.$group_icon->icon
			);

		$pusher = new Pusher(PUSHER_APP_KEY, PUSHER_APP_SECRET, PUSHER_APP_ID);
        $pusher->trigger('my-channel', 'accepted-group-request', $data);
        
        $notif = Notification::saveGroupRequest($user_id,$data);

		$result['status'] = true;
		echo json_encode($result);
		$this->view->disable();

	}
	public function requestAction()
	{
		$result = array();

		$data = array(
				"group_id" => $this->request->getPost('group_id'),
				"user_id" => $this->session->get('user_id')
			);

		$request = new GroupRequests();
		if(!$request->save($data)){
			$result['status'] = false;
			$message = '';
			foreach ($request->getMessages() as $value) {
				$message .= $value;
			}
			$result['messages'] = $message;
			echo json_encode($result);
			$this->view->disable();
			exit();
		}

		$result['status'] = true;
		echo json_encode($result);
		$this->view->disable();
	}

    public function indexAction()
    {

    	if($this->session->get('user_id') == null) $this->response->redirect(BASE_URI);

		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Group');
		$this->view->setVar('content', 'group/index');
		$this->view->setVar('title', 'Group Name');
		$group_id = $_GET['ghd'];

		$group_requests = GroupRequests::listRequests($group_id);
		$group = Groups::findFirstById($group_id);
		$cover_photo = ($group->cover_photo) ? $group->cover_photo : BASE_URI.'img/cover_photo/H-CoverPhoto.jpg';
		$group_organizers = GroupOrganizers::listOrganizers($group_id);
		$members = GroupMembers::listMembers($group_id,$this->session->get('user_id'));
		$incoming_events = Events::listIncomingEvents($group_id);
		$past_events = Events::listPastEvents($group_id);
		$icon = ($group->icon) ? IMAGE_URL_SMALL.$group->icon : IMAGE_URL_SMALL.DEFAULT_PIC;

		$this->view->setVar('icon',$icon);
		$this->view->setVar('group',$group);
		$this->view->setVar('cover_photo',$cover_photo);
		$this->view->setVar('group_requests',$group_requests);
		$this->view->setVar('group_organizers',$group_organizers);
		$this->view->setVar('group_members',$members);
		$this->view->setVar('incoming_events',$incoming_events);
		$this->view->setVar('past_events',$past_events);

		//SECURITY CHECK WHAT IS THE RELATIONSHIP OF THE USER TO THE GROUP
    	$user_id = $this->session->get('user_id');

    	// 1. CHECK IF ADMIN
    	$user_type = GroupOrganizers::findFirst(array(
    			'conditions'=>"user_id = ?0 and group_id = ?1",
    			'bind'=>array(0=>$user_id,1=>$group_id)
    		));

    	$type = "ordinary";
    	if($user_type)
    	{
    		$type = "admin";
    	}
    	else
    	{
    		$is_member = GroupMembers::findFirst(array(
                    'conditions'=>'user_id = ?0 and group_id =?1',
                    'bind'=>array(0=>$user_id,1=>$group_id)
                ));
    		if($is_member)
    		{
    			$type = "member";
    		}
    		else
    		{
    			if($group->privacy)
    			{
    				$type = "restricted";
    			}


    		}
    	}

    	$this->view->setVar("user_type",$type);


    }
    public function mygroupAction()
    {
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'MyGroup');
		$this->view->setVar('content', 'group/mygroup');
		$this->view->setVar('title', '');
    }
    public function createAction()
	{
		if(!$this->request->isPost()) die('What u doin');
		$result = array();
		$data = array(
				"name"=> $this->request->getPost('name'),
				"category"=> $this->request->getPost('category'),
				"privacy"=> $this->request->getPost('privacy'),
				"creator_id"=> $this->session->get('user_id')
			);
		$group = new Groups();
		if(!$group->save($data))
		{
			$result['status'] = false;
			$message = '';
			foreach ($group->getMessages() as $value) {
				$message .= $value;
			}
			$result['messages'] = $message;
			echo json_encode($result);
			$this->view->disable();
			exit();			
		}

		$organizers = new GroupOrganizers();
        $organizers->group_id = $group->id;
        $organizers->type = "admin";
        $organizers->user_id = $group->creator_id;
        @$organizers->save();

        $member =  new GroupMembers();
        $member->group_id = $group->id;
        $member->user_id = $group->creator_id;
        @$member->save();

		$result['status'] = true;
		echo json_encode($result);
		$this->view->disable();
	}

	public function listAction($user_id)
	{

		$groups = Groups::listGroup($user_id);
		$result = array();

		foreach ($groups as $value) {
			$result[] = array(
					'name'=>$value->name,
					'category'=>$value->category,
					'privacy'=>$value->privacy,
					'creator_id'=>$value->creator_id,
					'group_id'=>$value->id,
					'cover_photo'=>($value->cover_photo) ? $value->cover_photo : BASE_URI.'img/cover_photo/2.jpg',
					'icon'=>($value->icon) ? $value->icon : BASE_URI.'img/user_dp/steve.jpg',
					'total_past_events'=>Events::countPastEvents($value->id),
					'total_group_members'=>GroupMembers::countMembers($value->id),
					'total_incoming_events'=>Events::countIncomingEvents($value->id)
				);		
		}
		echo json_encode($result); 
		$this->view->disable();
	}
}

